'use strict';

function compute_median(collection) {

  let result;
  let mapped=collection.map(function(ele,i){
    return {inder:i,value:ele};
  });
  mapped.sort(function(a,b){
    return +(a.value>b.value)||+(a.value===b.value)-1;
  })
  let sortCollection=mapped.map(function(ele){
    return collection[ele.inder];
  })
  if(collection.length%2==0){
    result=(sortCollection[sortCollection.length/2] + sortCollection[sortCollection.length/2-1]) / 2;
  }
  else{
    result=sortCollection[parseInt(sortCollection.length/2)];
  }
  return result;


}

module.exports = compute_median;


